from numpy.ma.extras import average
from django_redis import get_redis_connection
from django.core.cache import cache
import json

from utils.querymysql import querymysql


def get_cached_query(cache_key, query, params=None, query_type='select'):
    """使用缓存包装查询函数"""
    try:
        # 使用django-redis获取数据
        cached_data = cache.get(cache_key)
        if cached_data is not None:
            return cached_data

        # 如果缓存中没有，执行查询
        data = querymysql(query, params or [], query_type)

        # 存入redis缓存，使用settings中配置的超时时间
        cache.set(cache_key, data)

        return data
    except Exception as e:
        print(f"Redis缓存错误: {e}")
        # 如果redis出错，直接执行查询
        return querymysql(query, params or [], query_type)


def getjobData():
    cache_key = 'jobData'
    return get_cached_query(cache_key, 'SELECT * FROM jobData')


def getjobData1():
    cache_key = 'jobData1'
    query = """
        SELECT * FROM jobData 
        ORDER BY RAND() 
        LIMIT 1000
    """
    return get_cached_query(cache_key, query)


def getjobData2():
    cache_key = 'jobData2'
    query = """
        SELECT *
        FROM jobData
        WHERE minSalary < 40000
        ORDER BY minSalary
    """
    return get_cached_query(cache_key, query)


def getaverageCity():
    cache_key = 'averageCity'
    query = """
        SELECT * FROM averageCity
    """
    return get_cached_query(cache_key, query)


def getsalaryCategory():
    cache_key = 'salaryCategory'
    query = """
        SELECT * FROM salaryCategory
    """
    return get_cached_query(cache_key, query)


def getexpSalary():
    cache_key = 'expSalary'
    query = """
        SELECT * FROM expSalary
    """
    return get_cached_query(cache_key, query)


def getaddressSum():
    cache_key = 'addressSum'
    query = """
        SELECT * FROM addressSum
    """
    return get_cached_query(cache_key, query)


def getpeopleCategory():
    cache_key = 'peopleCategory'
    query = """
        SELECT * FROM peopleCategory
    """
    return get_cached_query(cache_key, query)


def getsalaryTop():
    cache_key = 'salaryTop'
    return get_cached_query(cache_key, 'SELECT * FROM salaryTop')


def gettypeSalary(range):
    cache_key = f'typeSalary_{range}'
    sql_query = "SELECT type, `{}` FROM typeSalary".format(range)
    return get_cached_query(cache_key, sql_query)


def getaverageType():
    cache_key = 'averageType'
    return get_cached_query(cache_key, 'SELECT * FROM averageType')


def getaverageExperience():
    cache_key = 'averageExperience'
    return get_cached_query(cache_key, 'SELECT * FROM averageExperience')


def geteducationCount():
    cache_key = 'educationCount'
    return get_cached_query(cache_key, 'SELECT * FROM educationCount')


def gettypeCount():
    cache_key = 'typeCount'
    return get_cached_query(cache_key, 'SELECT * FROM typeCount')


def gettypeMax():
    cache_key = 'typeMax'
    return get_cached_query(cache_key, 'SELECT * FROM typeMax')


def getcitySalary():
    cache_key = 'citySalary'
    return get_cached_query(cache_key, 'SELECT * FROM citySalary')


def getcityPeople():
    cache_key = 'cityPeople'
    return get_cached_query(cache_key, 'SELECT * FROM cityPeople')

